package com.ian.homework;

import java.util.Random;

public class ArrayHomework {
    private final int iWidth;
    private final int iHeight;

    public int getiWidth() {
        return iWidth;
    }

    public int getiHeight() {
        return iHeight;
    }

    public int[][] getiArrayData() {
        return iArrayData;
    }

    public void setiArrayData(int[][] iArrayData) {
        this.iArrayData = iArrayData;
    }

    private int[][] iArrayData;

    public ArrayHomework() {
        this.iHeight = 16;
        this.iWidth = 16;
        //默认随机数组
        this.initRedomArray(30);
    }

    public ArrayHomework(int iWidth, int iHeight) {
        this.iWidth = iWidth;
        this.iHeight = iHeight;
    }

    public void showArrayData() {
        if (iArrayData == null) {
            throw new IllegalArgumentException("数组不能为空，请初始化该数组");
        }
        for (int i = 0; i < this.iHeight; i++) {
            for (int j = 0; j < this.iWidth; j++) {
                System.out.print(iArrayData[i][j] + "\t");
            }
            System.out.print("\n");
        }
    }

    /**
     * @param iSeed 随机种子
     */
    public void initRedomArray(int iSeed) {
        System.out.println("初始化随机数组,随机种子为: " + iSeed);
        int[][] iArrayDataTemp = new int[this.getiHeight()][this.getiWidth()];
        Random randData = new Random();

        for (int i = 0; i < this.iHeight; i++) {
            int iCX = 0;
            for (int j = 0; j < this.iWidth; j++) {
                iArrayDataTemp[i][j] = randData.nextInt(iSeed);
                System.out.print(iArrayDataTemp[i][j] + "\t");
                iCX += iArrayDataTemp[i][j];
            }
            System.out.print("\n");
            System.out.println("第" + i + "行的和为：" + iCX);
        }
        setiArrayData(iArrayDataTemp);
    }

    public void initZeroArray() {
        System.out.println("初始化空数,即全员为0");
        int[][] iArrayDataTemp = new int[this.getiHeight()][this.getiWidth()];
        Random randData = new Random();

        for (int i = 0; i < this.iWidth; i++) {
            for (int j = 0; j < this.iWidth; j++) {
                iArrayDataTemp[i][j] = 0;
                System.out.print(iArrayDataTemp[i][j] + "\t");
            }
            System.out.println();
        }
        setiArrayData(iArrayDataTemp);
    }

    public void setArrayDataFromIndex(int x, int y, int iData) {
        if (iArrayData == null) {
            throw new IllegalArgumentException("数组不能为空，请初始化该数组");
        }
        if (x >= iHeight || y >= iWidth) {
            System.out.println("数组越界");
            return;
        }
        this.iArrayData[x][y] = iData;

    }

    public void showArrayDataFromIndex(int x, int y) {
        if (iArrayData == null) {
            throw new IllegalArgumentException("数组不能为空，请初始化该数组");
        }
        if (x >= iHeight || y >= iWidth) {
            System.out.println("数组越界");
            return;
        }
        System.out.println("显示数组索引为x: " + x + " y: " + y + "的值为" + this.iArrayData[x][y]);
    }

    public void getArrayPostiveDiagonal() {
        if (iArrayData == null) {
            throw new IllegalArgumentException("数组不能为空，请初始化该数组");
        }
        int iCount = 0;
        System.out.println("从左到右的对角元素为:");
        for (int i = 0; i < this.iHeight; i++) {
            for (int j = 0; j < this.iWidth; j++) {

                if (i == j) {
                    iCount += iArrayData[i][j];
                    System.out.print(iArrayData[i][j] + "\t");
                } else {
                    System.out.print(" " + "\t");
                }

            }
            System.out.println();
        }
        System.out.println("从左到右的对角和为：" + iCount);

    }

    public void getArrayNegativeDiagonal() {
        if (iArrayData == null) {
            throw new IllegalArgumentException("数组不能为空，请初始化该数组");
        }
        int iCount = 0;
        System.out.println("从右到左的对角元素为:");
        for (int i = 0; i < this.iHeight; i++) {
            for (int j = 0; j < this.iWidth; j++) {
                int iIndex = iHeight - i;
                if (iIndex == j) {
                    iCount += iArrayData[i][j];
                    System.out.print(iArrayData[i][j] + "\t");
                } else {
                    System.out.print(" " + "\t");
                }
            }
            System.out.println();
        }
        System.out.println("从右到左的对角和为：" + iCount);

    }

}
